﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

using DAL;
using CTRL;

namespace GUI_Client
{
    public partial class GUIBookings : Form
    {
        private static GUIBookings instance;
        public static Booking Booking { get; private set; }
        private CtrlBooking bookingCtrl;
        private CtrlCar carCtrl;

        private GUIBookings()
        {
            this.bookingCtrl = CtrlBooking.Instance;
            this.carCtrl = CtrlCar.Instance;

            InitializeComponent();

            ShowTable();
        }

        public static GUIBookings Instance
        {
            get
            {
                if (instance == null)
                    instance = new GUIBookings();

                return instance;
            }
        }

        public void ShowTable()
        {
            dataGridView.Rows.Clear();
            List<Booking> list = bookingCtrl.GetAllBookingsOfCustomer(GUIMain.Customer.customerID);
            foreach (Booking book in list)
            {
                string licencePlate = carCtrl.GetCar(book.carID).licencePlate;
                dataGridView.Rows.Add(book.bookingID, book.bDate, licencePlate);
            }
            if (dataGridView.Rows.Count == 0)
            {
                noBookingsLbl.Visible = true;
                detailsBtn.Visible = false;
                returnBtn.Visible = false;
                takeBtn.Visible = false;
                cancelBtn.Visible = false;
            }
        }

        private void detailsBtn_Click(object sender, EventArgs e)
        {
            Booking = bookingCtrl.GetBooking(int.Parse
                (dataGridView.CurrentRow.Cells[0].Value.ToString()));
            ShowBookingDetails bookDetails = ShowBookingDetails.Instance;
            bookDetails.Show();
        }

        private void cancelBtn_Click(object sender, EventArgs e)
        {
            Booking booking = bookingCtrl.GetBooking(int.Parse
                (dataGridView.CurrentRow.Cells[0].Value.ToString()));
            if (bookingCtrl.CanCancelBooking(booking.bookingID))
            {
                bookingCtrl.CancelBooking(booking.bookingID);
                ShowTable();
                MessageBox.Show("The booking was successfully cancelled", "Suceeded",
                    MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
            else
                MessageBox.Show("This booking cannot be cancelled anymore because the battery/batteries has/have been either already taken or returned", "Failed",
                    MessageBoxButtons.OK, MessageBoxIcon.Error);
        }

        private void takeBtn_Click(object sender, EventArgs e)
        {
            TakeBatteries takeBatteries = TakeBatteries.Instance;
            takeBatteries.Show();
        }

        private void returnBtn_Click(object sender, EventArgs e)
        {
            ReturnBatteries returnBatteries = ReturnBatteries.Instance;
            returnBatteries.Show();
        }

        private void back_Click(object sender, EventArgs e)
        {
            GUIMain main = GUIMain.Instance;
            main.Show();

            this.Hide();
        }
    }
}